package com.landi.carkey.manager;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.GetCallback;
import com.landi.carkey.BuildConfig;
import com.landi.carkey.updata.UpdateService;
import com.landi.carkey.updata.VersionModel;
import com.landi.carkey.util.DialogFactory;

import java.io.File;


/**
 * @Description {具体做什么}
 * @Creator wushan
 * @Date 2016/12/29 14:57
 */

public class UpDataVersionManager {
    public static final String TABLE_VERSION = "version";

    private static UpDataVersionManager mInstance;
    private Activity context;

    public UpDataVersionManager(Activity context) {
        this.context = context;
    }

    public static UpDataVersionManager getInstance(Activity context) {
        if (mInstance == null) {
            mInstance = new UpDataVersionManager(context);
        }
        return mInstance;
    }

    public void checkVersion() {
        AVQuery<AVObject> query = new AVQuery<>(TABLE_VERSION);

        query.getFirstInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject av, AVException e) {

                if (e!=null){
                    Log.d("","*********checkVersion****挂了***"+e.getCode());
                }else{
                    if (av!=null){
                        Log.d("","*********checkVersion***当前版本****"+ BuildConfig.VERSION_CODE);
                        Log.d("","*********checkVersion*******"+av.toString());
                        int versionCode = av.getInt("versionCode");
                        String versionName = av.getString("versionName");
                        String versionSize = av.getString("versionSize");
                        String versionMsg = av.getString("versionMsg");
                        String loadUrl = av.getString("loadUrl");

                        VersionModel model = new VersionModel(versionCode,versionName,versionSize,versionMsg,loadUrl);
                        Log.d("","*********checkVersion*******"+versionCode+"  "+versionName+"  "+
                                versionSize+"  "+versionMsg+"  "+loadUrl);


                        if (BuildConfig.VERSION_CODE < versionCode ){
                            alertUpdate(model);
                        }
                    }
                }

            }
        });
    }
    public void alertUpdate(final VersionModel model) {
        DialogFactory.showUpdataVersionDialog(context, model, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(UpdateService.SAVAE_PATH + "/BicycleKey_"+model.getVersionName()+".apk");
                if (file.exists()) {
                    Uri uri = Uri.fromFile(file);
                    Intent intent2 = new Intent(Intent.ACTION_VIEW);
                    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent2.setDataAndType(uri, "application/vnd.android.package-archive");
                    context.startActivity(intent2);
                } else {
                    Intent intent = new Intent(context,
                            UpdateService.class);
                    intent.putExtra(UpdateService.KEY_NAME, "BicycleKey_"+model.getVersionName()+".apk");
                    intent.putExtra(UpdateService.KEY_URL, model.getLoadUrl());
                    context.startService(intent);
                }
                DialogFactory.dismissAlert(context);
            }
        });
    }

}
